//
//  StatusRetweetedView.swift
//  SinaWeibo25
//
//  Created by Apple on 16/3/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class StatusRetweetedView: UIView {
    
    //声明约束属性  约束对象类型 是 Constraint
    var bottomConstraint: Constraint?
    
    //数据模型
    var retweetedStatus: Status? {
        didSet {
            //绑定数据
            retweetedLabel.text = retweetedStatus?.text
            
            
            bottomConstraint?.uninstall()
            //根据是否有配图视图 动态的调整约束
            if let urls = retweetedStatus?.imageURLs where urls.count != 0 {
                print("原创微博有配图视图")
                //显示配图视图
                pictureView.hidden = false
                pictureView.imageURLs = urls
                //1.修改原创微博的底部约束: 相对于配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //constraint: 表示获取约束对象
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                print("原创微博没有++++++++配图视图")
                //隐藏配图视图
                pictureView.hidden = true
                pictureView.imageURLs = nil
                //相对于 微博正文
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 设置UI界面
    private func setupUI() {
        backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        addSubview(retweetedLabel)
        
        addSubview(pictureView)
        
        
        //设置约束
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatusCellMargin)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(retweetedLabel.snp_left)
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //给转发微博的底部添加约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    
    //MARK: 懒加载子视图
    private lazy var retweetedLabel: UILabel = UIFactory.labelFactory("听说下雨天,音乐和辣条更陪喔", textColor: UIColor.darkGrayColor(), fontSize: 14, alignment: .Left)
    
    //配图视图
    private lazy var pictureView: StatusPictureView = StatusPictureView()
}
